

package com.zdkj.subway.support.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.support.entity.SwPmInternetTicketEquipmentCode;
import com.zdkj.subway.support.entity.SwPmTvmCode;
import com.zdkj.subway.support.service.SwPmInternetTicketEquipmentCodeService;
import com.zdkj.subway.support.util.UploadDataListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * 互联网售票机编号基础参数表
 *
 * @author cuilei
 * @date 2020-05-28 14:03:40
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/swpminternetticketequipmentcode")
@Api(value = "swpminternetticketequipmentcode", tags = "互联网售票机编号基础参数表管理")
public class SwPmInternetTicketEquipmentCodeController {

    private final SwPmInternetTicketEquipmentCodeService swPmInternetTicketEquipmentCodeService;

    /**
     * 分页查询
     *
     * @param page                            分页对象
     * @param swPmInternetTicketEquipmentCode 互联网售票机编号基础参数表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwPmInternetTicketEquipmentCodePage(Page page, SwPmInternetTicketEquipmentCode swPmInternetTicketEquipmentCode) {
        if (!SecurityUtils.getUser().getBelongToLine()) {
            QueryWrapper<SwPmInternetTicketEquipmentCode> queryWrapper = Wrappers.query(swPmInternetTicketEquipmentCode);
            queryWrapper.lambda().eq(SwPmInternetTicketEquipmentCode::getLineCode, SecurityUtils.getUser().getLineCode());
            queryWrapper.lambda().eq(SwPmInternetTicketEquipmentCode::getStationCode, SecurityUtils.getUser().getStationCode());
            return R.ok(swPmInternetTicketEquipmentCodeService.page(page, queryWrapper));
        } else {
            return R.ok(swPmInternetTicketEquipmentCodeService.page(page, Wrappers.query(swPmInternetTicketEquipmentCode)));
        }
    }


    @ApiOperation(value = "查询启用状态的", notes = "查询启用状态的")
    @GetMapping("/equipmentCodeOfUsing")
    public R getInternetTicketEquipmentCodeOfUsing(SwPmInternetTicketEquipmentCode swPmInternetTicketEquipmentCode) {
        QueryWrapper<SwPmInternetTicketEquipmentCode> queryWrapper = Wrappers.query(swPmInternetTicketEquipmentCode);
        queryWrapper.lambda().eq(SwPmInternetTicketEquipmentCode::getLineCode, SecurityUtils.getUser().getLineCode());
        queryWrapper.lambda().eq(SwPmInternetTicketEquipmentCode::getStationCode, SecurityUtils.getUser().getStationCode());
        queryWrapper.lambda().eq(SwPmInternetTicketEquipmentCode::getStatus, "1");
        return R.ok(swPmInternetTicketEquipmentCodeService.list(queryWrapper));
    }

    /**
     * 通过id查询互联网售票机编号基础参数表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swPmInternetTicketEquipmentCodeService.getById(id));
    }

    /**
     * 新增互联网售票机编号基础参数表
     *
     * @param swPmInternetTicketEquipmentCode 互联网售票机编号基础参数表
     * @return R
     */
    @ApiOperation(value = "新增互联网售票机编号基础参数表", notes = "新增互联网售票机编号基础参数表")
    @SysLog("新增互联网售票机编号基础参数表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_internet_ticket_equipment_code_add')")
    public R save(@RequestBody SwPmInternetTicketEquipmentCode swPmInternetTicketEquipmentCode) {
        LambdaQueryWrapper<SwPmInternetTicketEquipmentCode> lambdaQueryWrapper=Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(SwPmInternetTicketEquipmentCode::getEquipmentCode,swPmInternetTicketEquipmentCode.getEquipmentCode());
        lambdaQueryWrapper.eq(SwPmInternetTicketEquipmentCode::getLineCode,swPmInternetTicketEquipmentCode.getLineCode());
        lambdaQueryWrapper.eq(SwPmInternetTicketEquipmentCode::getStationCode,swPmInternetTicketEquipmentCode.getStationCode());
        SwPmInternetTicketEquipmentCode codeswPmTvmCashboxCode=swPmInternetTicketEquipmentCodeService.getOne(lambdaQueryWrapper);
        if(ObjectUtil.isNotEmpty(codeswPmTvmCashboxCode)){
            return R.failed(codeswPmTvmCashboxCode,codeswPmTvmCashboxCode.getEquipmentCode()+"已存在,不能重复添加");
        }
        SwPmInternetTicketEquipmentCode defaultObj = ObjectUtils.getDefaultObj(SwPmInternetTicketEquipmentCode.class);
        BeanUtils.copyProperties(swPmInternetTicketEquipmentCode, defaultObj,
                ObjectUtils.getNullAndEmptyPropertyNames(swPmInternetTicketEquipmentCode));
        return R.ok(swPmInternetTicketEquipmentCodeService.save(defaultObj));
    }

    /**
     * 修改互联网售票机编号基础参数表
     *
     * @param swPmInternetTicketEquipmentCode 互联网售票机编号基础参数表
     * @return R
     */
    @ApiOperation(value = "修改互联网售票机编号基础参数表", notes = "修改互联网售票机编号基础参数表")
    @SysLog("修改互联网售票机编号基础参数表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_internet_ticket_equipment_code_edit')")
    public R updateById(@RequestBody SwPmInternetTicketEquipmentCode swPmInternetTicketEquipmentCode) {
        return R.ok(swPmInternetTicketEquipmentCodeService.updateById(swPmInternetTicketEquipmentCode));
    }

    /**
     * 通过id删除互联网售票机编号基础参数表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除互联网售票机编号基础参数表", notes = "通过id删除互联网售票机编号基础参数表")
    @SysLog("通过id删除互联网售票机编号基础参数表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('support_sw_pm_internet_ticket_equipment_code_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(swPmInternetTicketEquipmentCodeService.removeById(id));
    }

    /**
     * 导入互联网售票机编号
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入互联网售票机编号", notes = "导入互联网售票机编号")
    @SysLog("导入互联网售票机编号")
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SwPmInternetTicketEquipmentCode.class,
                    new UploadDataListener(swPmInternetTicketEquipmentCodeService, SwPmInternetTicketEquipmentCode.class)).sheet().doRead();
            return R.ok("导入成功");
        } catch (IOException e) {
            log.info(e.getMessage());
            return R.ok("导入失败");
        }
    }
}
